import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from keras import Sequential
from keras.layers import Dense, Dropout
from keras.utils import np_utils

data = pd.read_csv("dataset/cleveland.csv")
data = np.array(data)
x_train = data[:200, :-1]
y_train = data[:200, -1]
x_test = data[200:, :-1]
y_test = data[200:, -1]

mean = x_train.mean(axis=0)
std = x_train.std(axis=0)
x_train = (x_train - mean) / std

mean = x_test.mean(axis=0)
std = x_test.std(axis=0)
x_test = (x_test - mean) / std

y_train = np_utils.to_categorical(y_train, num_classes=2)
y_test = np_utils.to_categorical(y_test, num_classes=2)

model = Sequential()
model.add(Dense(512, input_shape=(x_train.shape[1],), activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(2, activation='sigmoid'))

# 模型编译
model.compile(optimizer='rmsprop',
              loss="categorical_crossentropy",
              metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=128, epochs=100, validation_split=0.01)

score = model.evaluate(x_test)
# print("模型的评估结果为：", score)

plt.plot(history.history['loss'])
plt.plot(history.history['accuracy'])
plt.legend(['loss', 'accuracy'])
plt.show()
